#include "System.h"

using namespace std;

void System::run()
{
    int op = -1;
    while (1) {
        op = menu();

        switch (op)
        {
        case 0://管理员登录
            managerlogin();
            break;
        case 1://用户登录
            userlogin();
            break;
        case 2:
            regist();//注册
            break;
        case 3:
            exit();//退出
            return;
            break;

        default:
            break;
        }
    }

}

int System::menu()
{
    //背景图和logo设置
    IMAGE bkimg, logo1, logo2;
    loadimage(&bkimg, "./01.jpg", 375, 812);
    putimage(0, 0, &bkimg);
    loadimage(&logo1, "./logo黑底.png", 166, 166);
    loadimage(&logo2, "./logo白底.png", 166, 166);
    putimage(104, 127, &logo2, SRCAND);
    putimage(104, 127, &logo1, SRCPAINT);


    //按钮设置
    BasicButton btn1(64, 353, 245, 64, 10, "管理员登录");
    btn1.setbtncl(96, 211, 225);
    btn1.settext(0, 72, 125, 32, 105, 367);
    btn1.show();

    BasicButton btn2(64, 453, 245, 64, 10, "用户登录");
    btn2.setbtncl(96, 211, 225);
    btn2.settext(0, 72, 125, 32, 125, 467);
    btn2.show();

    BasicButton btn3(112, 572, 152, 64, 10, "注册");
    btn3.setbtncl(47, 205, 14);
    btn3.settext(255, 255, 255, 32, 157, 586);
    btn3.show();

    BasicButton btn4(112, 672, 152, 64, 10, "退出");
    btn4.setbtncl(255, 255, 255);
    btn4.settext(94, 117, 136, 32, 157, 685);
    btn4.show();

    //持续循环直到获取鼠标点击按钮的信号
    while (1) {
        if (Window::havemsg()) {
            m_msg = Window::getmsg();
            btn1.setmsg(m_msg);
            if (btn1.isCliked()) {
                return 0;
            }
            btn2.setmsg(m_msg);
            if (btn2.isCliked()) {
                return 1;
            }
            btn3.setmsg(m_msg);
            if (btn3.isCliked()) {
                return 2;
            }
            btn4.setmsg(m_msg);
            if (btn4.isCliked()) {
                return 3;
            }
        }
    }

}

//管理员登录输入页面
void System::managerlogin()
{
    //背景图设置
    IMAGE bkimg;
    loadimage(&bkimg, "./01.jpg", 375, 812);
    putimage(0, 0, &bkimg);

    //按钮设置
    BasicButton btn1(109, 488, 152, 64, 10, "登录");
    btn1.setbtncl(96, 211, 225);
    btn1.settext(0, 72, 125, 32, 156, 502);
    btn1.show();

    BasicButton btn2(109, 595, 152, 64, 10, "返回");
    btn2.setbtncl(255, 255, 255);
    btn2.settext(94, 117, 136, 32, 156, 609);
    btn2.show();

    //文本框设置
    Textbox tb1(61, 211, 248, 54, "ID：", 66);
    tb1.settitle("请输入ID：");
    tb1.show();
    Textbox tb2(61, 324, 248, 54, "密码：", 101);
    tb2.settitle("请输入密码：");
    tb2.show();


    //持续循环直到获取鼠标点击按钮的信号
    while (1) {
        if (Window::havemsg()) {
            m_msg = Window::getmsg();
            btn1.setmsg(m_msg);
            if (btn1.isCliked()) { //点击了登录按钮，调用管理员主页面函数
                string ID = tb1.gettext();
                string password = tb2.gettext(); //获取输入的用户名和密码

                ID = Login(ID, password, 1);
                if (ID == "Wrong password") {
                    tb2.outRemain(ID);
                }
                else if (ID == "Can't find ID") {
                    tb1.outRemain(ID);
                }
                else { //登录成功跳出循环
                    break;
                }
            }
            btn2.setmsg(m_msg);
            if (btn2.isCliked()) { //点击了返回按钮，返回到初始run框架
                return;
            }
            tb1.setmsg(m_msg);
            if (tb1.isCliked()) {
                tb1.updatetext(0);
            }
            tb2.setmsg(m_msg);
            if (tb2.isCliked()) {
                tb2.updatetext(0);
            }
        }
    }

    //登录成功跳出循环，进入管理员页面功能骨架
    managerMainGujia1();
}

//用户登录输入界面
void System::userlogin()
{
    //背景图设置
    IMAGE bkimg;
    loadimage(&bkimg, "./01.jpg", 375, 812);
    putimage(0, 0, &bkimg);

    //按钮设置
    BasicButton btn1(109, 488, 152, 64, 10, "登录");
    btn1.setbtncl(96, 211, 225);
    btn1.settext(0, 72, 125, 32, 156, 502);
    btn1.show();

    BasicButton btn2(109, 595, 152, 64, 10, "返回");
    btn2.setbtncl(255, 255, 255);
    btn2.settext(94, 117, 136, 32, 156, 609);
    btn2.show();

    //文本框设置
    Textbox tb1(61, 211, 248, 54, "ID：", 66);
    tb1.settitle("请输入ID：");
    tb1.show();
    Textbox tb2(61, 324, 248, 54, "密码：", 101);
    tb2.settitle("请输入密码：");
    tb2.show();


    //持续循环直到获取鼠标点击按钮的信号
    while (1) {
        if (Window::havemsg()) {
            m_msg = Window::getmsg();
            btn1.setmsg(m_msg);
            if (btn1.isCliked()) { //点击了登录按钮，调用用户主页面函数
                string ID = tb1.gettext();
                string password = tb2.gettext(); //获取输入的用户名和密码

                ID = Login(ID, password, 1);
                if (ID == "Wrong password") {
                    tb2.outRemain("密码错误");
                }
                else if (ID == "Can't find ID") {
                    tb1.outRemain("不存在此ID");
                }
                else { //登录成功跳出循环
                    break;
                }
            }
            btn2.setmsg(m_msg);
            if (btn2.isCliked()) { //点击了返回按钮，返回到初始run框架
                return;
            }
            tb1.setmsg(m_msg);
            if (tb1.isCliked()) {
                tb1.updatetext(0);
            }
            tb2.setmsg(m_msg);
            if (tb2.isCliked()) {
                tb2.updatetext(0);
            }
        }
    }

    //登录成功跳出循环，进入用户页面功能骨架
    userMainGujia();
}

//注册输入页面
void System::regist()
{
    //背景图设置
    IMAGE bkimg;
    loadimage(&bkimg, "./01.jpg", 375, 812);
    putimage(0, 0, &bkimg);

    //按钮设置
    BasicButton btn1(109, 488, 152, 64, 10, "注册");
    btn1.setbtncl(47, 205, 14);
    btn1.settext(255, 255, 255, 32, 156, 502);
    btn1.show();

    BasicButton btn2(109, 595, 152, 64, 10, "退出");
    btn2.setbtncl(255, 255, 255);
    btn2.settext(94, 117, 136, 32, 156, 609);
    btn2.show();

    //文本框设置
    Textbox tb1(61, 146, 248, 54, "ID：", 66);
    tb1.settitle("请设置ID（10位的数字）：");
    tb1.show();
    Textbox tb2(61, 233, 248, 54, "用户名：", 121);
    tb2.settitle("请设置用户名：");
    tb2.show();
    Textbox tb3(61, 319, 248, 54, "密码：", 101);
    tb3.settitle("请设置密码：");
    tb3.show();


    //持续循环直到获取鼠标点击按钮的信号
    while (1) {
        if (Window::havemsg()) {
            m_msg = Window::getmsg();
            btn1.setmsg(m_msg);
            if (btn1.isCliked()) { //点击了登录按钮，调用管理员主页面函数
                string ID = tb1.gettext();
                string username = tb2.gettext();
                string password = tb3.gettext(); //获取输入的ID，用户名和密码
                int flag = enroll(ID, username, password, 1);
                if (flag == 1) {
                    break; //注册成功则跳出循环，并弹出提示框
                }
                else if (flag == 0) { //ID重复
                    tb1.outRemain("已存在此ID");
                }
                else { //ID不符合要求
                    tb1.outRemain("ID不符要求");
                }
            }
            btn2.setmsg(m_msg);
            if (btn2.isCliked()) { //点击了返回按钮，返回到初始run框架
                return;
            }
            tb1.setmsg(m_msg); //ID
            if (tb1.isCliked()) {
                tb1.updatetext(0);
            }
            tb2.setmsg(m_msg); //username
            if (tb2.isCliked()) {
                tb2.updatetext(0);
            }
            tb3.setmsg(m_msg); //password
            if (tb3.isCliked()) {
                tb3.updatetext(0);
            }
        }
    }
    //注册成功则跳出上面的循环，弹出提示框，关闭弹窗后返回初始页面
    setfillcolor(RGB(255, 255, 255)); //设置矩形白色
    fillroundrect(22, 177, 353, 373, 10, 10);
    settextcolor(RGB(0, 0, 0));
    //设置文本大小，垂直压缩，字体
    settextstyle(32, 0, "黑体");
    //去除文字背景
    setbkmode(TRANSPARENT);
    outtextxy(102, 214, "注册成功！");
    BasicButton btn(129, 284, 118, 64, 10, "确定");
    btn.setbtncl(96, 211, 225);
    btn.settext(0, 72, 125, 32, 152, 298);
    btn.show();
    while (1) {
        if (Window::havemsg()) {
            m_msg = Window::getmsg();
            btn.setmsg(m_msg);
            if (btn.isCliked()) { //点击了弹窗的确定按钮，返回到初始run框架
                return;
            }
        }
    }
}

void System::exit()
{
    closegraph();//关闭窗口
    return;
}
